# coding=utf-8

"""
    Created by Kalyter on 2017-12-13.
"""


class Config(dict):
    """配置类"""

    def from_object(self, obj):
        """从对象中加载配置。

        不接受字典对象，因为字典对应的键并不是它的属性，它的值。
        """

        for key in dir(obj):
            if key.isupper():
                self[key] = getattr(obj, key)

    def from_mapping(self, mapping, **kwargs):
        """从类似于字典的结构中更新配置"""

        mappings = []
        if hasattr(mapping[0], 'items'):
            mappings.append(mapping[0].items())
        else:
            mappings.append(mapping[0])
        mappings.append(kwargs.items())
        for mapping in mappings:
            for key, value in mapping:
                if key.isupper():
                    self[key] = value
